#include <stdio.h>
#include <string.h>

#define LINEEND 0xfffffff
#define LINELENGTH 101

#define CUR 0x0
#define START 0x1
#define END 0x2

typedef int Array[LINELENGTH];

/* create a empty line list */
void InitList(Array LineList)
{
	memset(LineList, 0, sizeof(int) * LINELENGTH);
	LineList[0] = LINEEND;
}

void DestroyList(Array LineList)
{
	/* the list whether exist or not */
	/* to do start */

	/* to do end */
	memset(LineList, 0, sizeof(LineList));
}

void ClearList(Array LineList)
{
	/* the list whether exist or not */
	/* to do start */

	/* to do end */
	memset(LineList, 0, sizeof(LineList));
	LineList[0] = LINEEND;
}

int ListEmpty(Array LineList)
{
	if (LineList[0] == LINEEND)
		return 1;
	else
		return 0;
}

int ListLength(Array LineList)
{
	int Length, i;

	/* the list whether exist or not */
	/* to do start */

	/* to do end */

	for (i = 0; i < LINELENGTH; i++) {
		if (LineList[i] == LINEEND)
			return i;
	}

	return -1;
}

int GetElem(Array LineList, int Num)
{
	/* the list whether exist or not */
	/* to do start */

	/* to do end */
	return LineList[Num];
}

void LocateElem()
{
	/* the list whether exist or not */
	/* to do start */

	/* to do end */
	
}

int PriorElem(Array LineList, int CurElem)
{
	/* the list whether exist or not */
	/* to do start */

	/* to do end */
	int i;
	for (i = 0; i < LINELENGTH; i++) {
		if (LineList[i] == CurElem)
			return LineList[i - 1];
	}
}

int NextElem(Array LineList, int CurElem)
{
	/* the list whether exist or not */
	/* to do start */

	/* to do end */
	int i;
	for (i = 0; i < LINELENGTH; i++) {
		if (LineList[i] == CurElem)
			return LineList[i + 1];
	}

}

int ListInsert(Array LineList, int Data, int Command, int Pos)
{
	/* the list whether exist or not */
	/* to do start */

	/* to do end */
	int i, Length;

	switch (Command) {
		case CUR:
			if (Pos < 0 || Pos > 99) {
				printf("%s:Pos should 0 - %d\n", __func__, LINELENGTH - 2);
				return -1;
			}
			Length = ListLength(LineList);
			if (Length <= LINELENGTH - 2) {
				for (i = Length + 1; i > Pos; i--) {
					LineList[i] = LineList[i -1];
				}
				LineList[Pos] = Data;
			}
			else {
				printf("%s:insert cur error!memory full,length = %d\n", __func__,LINELENGTH);
				return -1;
			}
			break;
		case START:
			Length = ListLength(LineList);
			if (Length <= LINELENGTH - 2) {
				for (i = Length + 1; i > 0; i--) {
					LineList[i] = LineList[i -1];
				}
			}
			else {
				printf("%s:insert start error!memory full,length = %d\n", __func__, LINELENGTH);
				return -1;
			}
			LineList[0] = Data;
			break;
		case END:
			Length = ListLength(LineList);
			if (Length <= LINELENGTH - 2) {
				LineList[Length + 1] = LINEEND;
				LineList[Length] = Data;
			}
			else {
				printf("%s:insert end error!memory full,length = %d\n", __func__,LINELENGTH);
				return -1;
			}
			break;
		default:
			break;
	}
	
}

void ListCpy(Array LineDest, Array LineSrc)
{
	int i;

	for (i = 0; i < LINELENGTH - 1; i++)
		LineDest[i] = LineSrc[i];
}

void ListSort(Array Src)
{
	int i, j, Tmp, SrcLength;

	SrcLength = ListLength(Src);
	printf("SrcLength = %d\n", SrcLength);

	for (i = 0; i < SrcLength - 1; i++) {
		for (j = 0; j < SrcLength - 1 - i; j++) {
			if(Src[j] > Src[j + 1]) {
				Tmp = Src[j];
				Src[j] = Src[j + 1];
				Src[j + 1] = Tmp;
			}
		}
	}
}

void ListDelete();
void ListTraverse();


/*
int Together(int &a, int &b)
{
	int ALen, BLen, i;

	ALen = ListLength(a);
	BLen = ListLength(b);

	for (i = 1; i <= BLen; i++) {
		GetElem(b, i, e);
		if(!LocateElem())
			ListInsert();
	}
}*/
void DebugPirnt(Array LineList)
{
	int i;

	printf("LineList Debug:\n");
	for (i = 0; i < LINELENGTH; i++) {
		if (i % 5 == 0)
			printf("\n");
		printf("[%2d]:%-10d ", i, LineList[i]);
	}
	printf("\n");
}

int LineBHaveLineAElem(Array LineB, int LineAElem)
{
	int LengthB, i;
	
	LengthB = ListLength(LineB);

	for (i = 0; i < LengthB; i++) {
		if (LineB[i] == LineAElem)
			return 1;
	}
	return 0;
}

void MergeTwoLists(Array First, Array Second, Array LineTogether)
{
	int SecondLen, i;

	SecondLen = ListLength(Second);
	InitList(LineTogether);
	ListCpy(LineTogether, First);

	for (i = 0; i < SecondLen - 1; i++) {
		if (!LineBHaveLineAElem(LineTogether, GetElem(Second, i))) {
			printf("LineA[%d] = %d\n", i, Second[i]);
			ListInsert(LineTogether,Second[i],END,0);
		}
	}
}

	
int main()
{
	Array LineA, LineB, LineTogether;
	int LengthA, i;

	InitList(LineA);
	InitList(LineB);
	InitList(LineTogether);
	
	for (i = 0; i < 50; i++) {
		ListInsert(LineA, i, START, 0);
	}

	for (i = 0; i < 50; i++) {
		ListInsert(LineB, i + 10, START, 0);
	}

	MergeTwoLists(LineA,LineB,LineTogether);

	printf("Sort Before:LineTogether = \n");
	DebugPirnt(LineTogether);

	ListSort(LineTogether);
	printf("Sort After:LineTogether = \n");
	DebugPirnt(LineTogether);

	return 0;
}
